#!/bin/sh

# This is a qemu-ifdown script for bridging.
# You can use it when starting a KVM guest with bridge mode network.
# set your bridge name
#
# (C) 2019.09.06 BiscuitOS <buddy.zhang@aliyun.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
BRIDGE=bsBridge0
TAP=$1

NET_CFG=/tmp/BiscuitOS_NetCfg.cfg

[ ! -f ${NET_CFG} ] && echo "${NET_CFG} doesn't exist" && exit 0
uGW=`sed -n 2p ${NET_CFG}`
uBG=`sed -n 1p ${NET_CFG}`
uTA=`sed -n 4p ${NET_CFG}`
uIC=`sed -n 3p ${NET_CFG}`
DEFAULT_GW=${uGW#* }
BRIDGE_IP=${uBG#* }
TAP_IP=${uTA#* }
NIC=${uIC#* }

# Remove TAP
sudo ifconfig ${TAP} down
sudo tunctl -d ${TAP} > /dev/null 2>&1

# Remove route

# Remove TAP from Bridge
sudo brctl delif ${BRIDGE} ${TAP}
sudo ifconfig ${BRIDGE} down

if [ ${TAP} != "bsTap0" ]; then
	sudo brctl stp ${BRIDGE} off
	sudo brctl delbr ${BRIDGE}

	# Remove leacy NETcfg file
	rm -rf ${NET_CFG} > /dev/null 2>&1
fi
echo "[BiscuitOS] Closing BiscuitOS TAP: $1"
